package com.alipay.controller;

import com.alipay.exception.BusinessException;
import com.alipay.model.Response;
import com.alipay.model.dto.SendSuccessDto;
import com.alipay.model.enums.ResponseCodeEnum;
import com.alipay.service.CallbackService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 *
 * @author Jun
 * @date 2019/3/23
 */
@Slf4j
@RestController
@RequestMapping(value = "/callback")
public class CallbackController {

    @Autowired
    private CallbackService callbackService;

    /**
     * 发红包转账成功后回调此接口
     * @param sendSuccessDto
     * @return
     */
    @PostMapping("/send")
    public Response sendSuccess(@RequestBody @Valid SendSuccessDto sendSuccessDto){
        try {
            callbackService.sendSuccessCallBack(sendSuccessDto);
            return Response.newSuccess(sendSuccessDto.getRequestNo(), null);
        }catch (BusinessException e){
            // 业务报错
            log.warn("sendSuccess BusinessException, requestNo={}, e={}", sendSuccessDto.getRequestNo(), ExceptionUtils.getStackTrace(e));
            return Response.newFailure(sendSuccessDto.getRequestNo(), e.getCode(), e.getMessage());
        }catch (Exception e){
            log.error("sendSuccess Exception, requestNo={}, e={}", sendSuccessDto.getRequestNo(), ExceptionUtils.getStackTrace(e));
        }
        return Response.newFailure(sendSuccessDto.getRequestNo(), ResponseCodeEnum.UNKNOWN_ERROR);
    }
}
